﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Diagnostics.Eventing.Reader;

namespace COC_helper
{
    public partial class Relate : Form
    {
        public Relate()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Relate_Shown(object sender, EventArgs e)
        {
            this.BackColor = Color.FromArgb(255, 244, 192);
            try
            {
                //Debug.WriteLine(Global_Value.ZMB_PATH + "rela.json");
                //string json = File.ReadAllText(Global_Value.ZMB_PATH + "rela.json");
                //List<List<string>> data = JsonConvert.DeserializeObject<List<List<string>>>(Global_Value.zmb_Setting.relaText);
                List<List<string>> data = Global_Value.zmb_Setting.relaText;
                RelaTag _myTag = Global_Value.zmb_Setting.relaTag;
                if (_myTag == null)
                {
                    _myTag = new RelaTag();
                    _myTag.No1_Num = 3;
                    _myTag.Name2 = "A";
                    _myTag.Name1 = "B";
                }
                label1.Text = _myTag.Name1 + "团结度";
                label2.Text = _myTag.Name2 + "团结度";
                label3.Text = _myTag.Name1 + '_' + _myTag.Name2 + "关系系数";
                label4.Text = _myTag.Name2 + '_' + _myTag.Name1 + "关系系数";
                label5.Text =  _myTag.Name2+label5.Text+_myTag.No1_Num.ToString();
                Button_A.Text = _myTag.Name1;
                Button_B.Text = _myTag.Name2;
                label5.Tag = _myTag.No1_Num.ToString();

                Tabel.Rows.Clear();
                if (data == null || data.Count == 0)
                {
                    MessageBox.Show("JSON文件中没有有效数据", "信息",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Global_Value.MyLogHandle.LogPush("派系关系表示器:" + "JSON文件中没有有效数据", "Wran");
                    return;
                }
                int maxColumns = 0;
                foreach (var row in data)
                {
                    if (row.Count > maxColumns) maxColumns = row.Count;
                }
                int _PeoSum = 0,_temp = 0,_OreSum = 0,_con1 = 0,_con2 = 0;
                int num1 = 0,num2 = 0,num3 = 0,num4 = 0;
                float _temp1;
                foreach (var rowData in data)
                {
                    // 创建新行
                    int rowIndex = Tabel.Rows.Add();

                    // 填充单元格
                    for (int colIndex = 0; colIndex < rowData.Count; colIndex++)
                    {
                        if (colIndex < maxColumns) // 确保不超过列数
                        {
                            Tabel.Rows[rowIndex].Cells[colIndex].Value = rowData[colIndex];
                            if (rowIndex <= _myTag.No1_Num && colIndex <= _myTag.No1_Num)
                            { Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.FromArgb(255,128,128); }
                            else if(rowIndex > _myTag.No1_Num && colIndex > _myTag.No1_Num)
                            { Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.FromArgb(128,255,128); }
                            else
                            { Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.Yellow; }
                            if (rowIndex == colIndex || rowIndex == 0 || colIndex == 0)
                                Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.White;
                            try
                            {
                                if (Tabel.Rows[rowIndex].Cells[colIndex].Value.ToString() == "")
                                {
                                    Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.FromArgb(255,244,192);
                                    continue; 
                                }

                                _temp = int.Parse(Tabel.Rows[rowIndex].Cells[colIndex].Value.ToString());
                            }
                            catch (Exception)
                            {
                                Debug.WriteLine("转换失败：" + Tabel.Rows[rowIndex].Cells[colIndex].Value.ToString());
                                Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor = Color.FromArgb(255, 244, 192);
                                continue;
                                //throw;
                            }
                            if (Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor == Color.FromArgb(255, 128, 128))
                            { _OreSum += _temp;num2++; }
                            else if (rowIndex <= _myTag.No1_Num && Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor == Color.Yellow)
                            { _con1 += _temp; num3++; }

                            else if (Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor == Color.FromArgb(128, 255, 128))
                            { _PeoSum += _temp;num1++; }
                            else if (rowIndex > _myTag.No1_Num && Tabel.Rows[rowIndex].Cells[colIndex].Style.BackColor == Color.Yellow)
                            { _con2 += _temp;num4++; }
                            


                        }

                    }
                }
                try
                {
                    Debug.WriteLine("peo1:" + _PeoSum);
                    Debug.WriteLine("peo2:" + _OreSum);
                    Debug.WriteLine("peo3:" + _con1);
                    Debug.WriteLine("peo4:" + _con2);
                    textBox4.Text = (num1 ).ToString();
                    textBox1.Text = (num2).ToString();
                    textBox2.Text = (num3).ToString();
                    textBox3.Text = (num4).ToString();
                    _temp1 = ((float)_PeoSum / float.Parse(textBox4.Text));
                    linkLabel1.Text = _temp1.ToString();
                    _temp1 = ((float)_OreSum / float.Parse(textBox1.Text));
                    linkLabel2.Text = _temp1.ToString();
                    _temp1 = ((float)_con1 / float.Parse(textBox2.Text));
                    linkLabel3.Text = _temp1.ToString();
                    _temp1 = ((float)_con2 / float.Parse(textBox3.Text));
                    linkLabel4.Text = _temp1.ToString();
                }
                catch (Exception)
                {

                    return;
                }
               



    }
            catch (Exception err)
            {
                Debug.WriteLine("文件打开失败,文件可能不存在或者被占用");
                Global_Value.MyLogHandle.LogPush("派系关系表示器:" + err.ToString(), "Error");
                return;
                //throw;
            }
            


        }

        private void Relate_FormClosing(object sender, FormClosingEventArgs e)
        {
       
            //var data = new List<List<string>>();
            
            RelaTag temp = new RelaTag();
            temp.Name1 = Button_A.Text;
            temp.Name2 = Button_B.Text;
            temp.No1_Num = int.Parse(label5.Tag.ToString());
            Global_Value.zmb_Setting.relaTag = temp;  
            Global_Value.zmb_Setting.relaText.Clear();


            foreach (DataGridViewRow row in Tabel.Rows)
            {
                

                    var rowData = new List<string>();

                    
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                       
                        rowData.Add(cell.Value?.ToString() ?? string.Empty);
                    }

                Global_Value.zmb_Setting.relaText.Add(rowData);

                
            }


            //// 4. 序列化为 JSON
            //string json = JsonConvert.SerializeObject(data, Formatting.Indented);

            //Debug.Write(json);
           //Global_Value.zmb_Setting.relaText = json;
            
        }

        private void Button_A_MouseClick(object sender, MouseEventArgs e)
        {
            Button button = sender as Button;
            string temp = Interaction.InputBox("请输入要指定的派系群名称");
            if (button.Tag.ToString() == "A")
            {
                Button_A.Text = temp;
            }
            else
            { 
                Button_B.Text = temp;
            }
        }

        private void label5_Click(object sender, EventArgs e)
        {
            label5.Tag = Interaction.InputBox("输入数量");
        }
    }
}
